package com.erp.produce.controller;

import com.erp.common.ResponseMessage;
import com.erp.common.controller.BaseController;
import com.erp.produce.entity.Cooperatives;
import com.erp.produce.service.CooperativeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/coo")
@Api("外协企业接口")
public class CooperativeController implements BaseController<Cooperatives> {
    @Autowired
    CooperativeService service;

    @ApiOperation("返回所有外协企业")
    @GetMapping("/list")
    public ResponseMessage list(){
        List<Cooperatives> list = service.list();
        if (list.size()>0){
            return new ResponseMessage(list);
        } else
            return ResponseMessage.fail("无相关数据信息");
    }

    @ApiOperation("增加外协企业")
    @PostMapping("/add")
    @Override
    public ResponseMessage add(Cooperatives entity) {
        if (service.add(entity)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("添加外协企业失败");
    }

    @ApiOperation("删除外协企业（支持多选）")
    @PostMapping("/list/delete")
    @Override
    public ResponseMessage deleteSome(@RequestParam(value = "idArray[]")Long[] ids) {
        if (service.deleteIds(ids)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("删除外协企业失败，请重试");
    }

    @ApiOperation("更新外协企业信息")
    @PostMapping("/update")
    @Override
    public ResponseMessage update(Cooperatives entity) {
        if (service.update(entity)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("更新外协企业失败");
    }

    @ApiOperation("分页展示外协企业信息")
    @GetMapping("/list/{page}/{size}")
    @Override
    public ResponseMessage listPage(@PathVariable("page") int page, @PathVariable("size") int size) {
        List<Cooperatives> list = service.listPage(page, size);
        if (list.size() > 0) {
            return new ResponseMessage(list);
        }
        return ResponseMessage.fail("无相关外协企业");
    }

    @ApiOperation("分页展示外协企业信息（带模糊查询）")
    @GetMapping("/list/name/{page}/{size}")
    @Override
    public ResponseMessage listPageLike(@PathVariable("page") int page, @PathVariable("size") int size, String column) {
        List<Cooperatives> list = service.listPageLike(page, size, column);
        if (list.size() > 0) {
            return new ResponseMessage(list);
        }
        return ResponseMessage.fail("无相关外协企业");
    }
}
